package com.myflink.day04;

import com.myflink.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.AscendingTimestampExtractor;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @author Shelly An
 * @create 2020/9/19 11:23
 */
public class TimeCharacteristic_EventTime {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //1. 指定时间语义 枚举
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        DataStreamSource<String> socketDS = env.socketTextStream("master", 4444);
        SingleOutputStreamOperator<WaterSensor> sensorDS = socketDS
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] datas = value.split(",");
                        return new WaterSensor(datas[0], Long.valueOf(datas[1]), Integer.valueOf(datas[2]));
                    }
                })
                //2. 指定如何从数据中抽取出事件时间，注意：时间单位是ms
                .assignTimestampsAndWatermarks(
                        new AscendingTimestampExtractor<WaterSensor>() {
                            @Override
                            public long extractAscendingTimestamp(WaterSensor element) {
                                return element.getTs() * 1000L;
                            }
                        }
                );


        sensorDS
                .keyBy(WaterSensor::getId)
                .timeWindow(Time.seconds(5))
                .process(
                        //全窗口函数   整个窗口的数据存起来，关窗的时候后一次性一起计算
                        new ProcessWindowFunction<WaterSensor, Long, String, TimeWindow>() {
                    /**
                     * 计数
                     * @param s key
                     * @param context 上下文
                     * @param elements 整个窗口本组（key：s）的数据
                     * @param out 采集器
                     * @throws Exception
                     */
                    @Override
                    public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<Long> out) throws Exception {
                        for (WaterSensor element : elements) {
                            System.out.println(element.toString());
                        }
                        //.spliterator().estimateSize() 迭代器中数据的条数
                        out.collect(elements.spliterator().estimateSize());
                    }
                }).print();



        env.execute();
    }
}
